package www.wf.pro.service.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.wf.pro.code.RedisCode;
import www.wf.pro.entity.TokenEntity;
import www.wf.pro.model.AdminModel;
import www.wf.pro.model.ResultModel;
import www.wf.pro.request.AdminRequest;
import www.wf.pro.response.AdminResponse;
import www.wf.pro.service.AdminService;
import www.wf.pro.service.BaseService;
import www.wf.pro.utils.Md5Util;

import java.util.concurrent.TimeUnit;

/**
 * @author wang fei
 * @date 2020-07-29 17:34
 */
@Service
@Transactional
public class AdminServiceImpl extends BaseService implements AdminService {

    /**
     * 缓存两小时
     */
    protected final static Integer LOGIN_TIME_LONG = 7000;


    @Override
    public ResultModel<AdminResponse> login(AdminRequest request) {
        log.info("总后台登陆{}", JSON.toJSONString(request));

        if (request == null  || StringUtils.isBlank(request.getLoginName()) || StringUtils.isBlank(request.getPassWord())){
            ResultModel.fail("请输入管理员账号与密码~！");
        }

        AdminModel adminModel = adminMapper.select(new AdminModel(){{
            setLoginName(request.getLoginName());
        }});
        if (adminModel == null){
            ResultModel.fail("登陆账号无效~!");
        }
        if(adminModel.getIsDel() == 1){
            ResultModel.fail("用户已被删除~!");
        }
        if(!Md5Util.md5(request.getPassWord()).equals(adminModel.getPassWord())){
            ResultModel.fail("登陆密码错误~！");
        }
//
//        //创建token
//        TokenEntity entity = new TokenEntity();
//        entity.setId(adminModel.getAdminId().longValue());
//        //存入redis缓存中  键：ONE_STAND:USER:admin_token:D6DXD08rQ2_2    【最后的D6DXD08rQ2_2是生成的token】
//        /**
//         * 设定键值的过期时间。
//         * set(K key, V value, long timeout, TimeUnit unit)
//         *timeutils[时间颗粒度转换]seconds是秒。
//         */
//        redisUtil.set(RedisCode.getAdminToken(entity.getToken()), entity, LOGIN_TIME_LONG, TimeUnit.SECONDS);
        AdminResponse response = new AdminResponse();
        BeanUtils.copyProperties(request, response);
//        response.setToken(entity.getToken());
//        response.setPassWord(null);
        return ResultModel.success(response);
    }

    public ResultModel<Integer> register(AdminRequest request){
        log.info("注册以及修改账号:{}",JSON.toJSONString(request));
        if(request == null){
            ResultModel.checkParamFail();
        }else if(StringUtils.isBlank(request.getLoginName())){
            ResultModel.fail("请输入管理员账号~！");
        }

        AdminModel adminModel  = adminMapper.select(new AdminModel(){{
            setLoginName(request.getLoginName());
            setAdminId(request.getAdminId());
        }});

        if(adminModel != null){
            return ResultModel.fail("管理员账号重复~!");
        }

        AdminModel adminModel1 = new AdminModel();
        BeanUtils.copyProperties(request,adminModel1);
        /*注册*/
        if(request.getAdminId() == null){
            if(StringUtils.isBlank(request.getPassWord())){
                return ResultModel.fail("请输入管理员密码~!");
            }
            adminModel1.setPassWord(Md5Util.md5(request.getPassWord()));
            adminModel1.setCreateTime(currentDate());
            adminModel1.setIsDel(0);
            adminMapper.insertSelective(adminModel1);
            return ResultModel.success(adminModel1.getAdminId());
        }
        if(!StringUtils.isBlank(request.getPassWord())){
            adminModel1.setPassWord(Md5Util.md5(request.getPassWord()));
        }
        adminMapper.updateByPrimaryKeySelective(adminModel1);
        return ResultModel.success(adminModel1.getAdminId());
    }
}
